package com.project.daoImpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.project.dao.ProjectDao;
import com.project.entity.ProjectInfo;
import com.project.page.Page;
import com.project.utils.Utils;

public class ProjectDaoImpl extends Utils implements ProjectDao{
	//查询全部
	public List<ProjectInfo> query() {
		List<ProjectInfo> pros = new ArrayList<ProjectInfo>();
		String sql="SELECT * FROM project_info";
		rs = super.executeQuery(sql);
		try {
			while(rs.next()) {
				ProjectInfo pro = new ProjectInfo(rs.getInt("id"), rs.getString("project_name"), rs.getString("start_date"), rs.getString("end_date"), rs.getInt("status"));
				pros.add(pro);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, ps, rs);
		}
		return pros;
	}
	//按条件查询
	public List<ProjectInfo> querytj(ProjectInfo pro) {
		List<ProjectInfo> eys = new ArrayList<ProjectInfo>();
		String sql = "SELECT * FROM project_info WHERE id = ?";
		Object[] parms = {pro.getId()};
		rs=super.executeQuery(sql, parms);
		try {
			while(rs.next()) {
				ProjectInfo pro1 = new ProjectInfo(rs.getInt("id"), rs.getString("project_name"), rs.getString("start_date"), rs.getString("end_date"), rs.getInt("status"));
				eys.add(pro1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, ps, rs);
		}
		return eys;
	}
	//增加
	public int zeng(ProjectInfo pro) {
		String sql = "INSERT INTO project_info(project_name,start_date,end_date,status) VALUE(?,?,?,?)";
		Object[] parms = {pro.getProjectName(),pro.getStartDate(),pro.getEndDate(),pro.getStatus()};
		int line = super.executeUpdate(sql, parms);
		return line;
	}
	//删除
	public int shan(ProjectInfo pro) {
		String sql = "DELETE FROM project_info WHERE id = ?";
		Object[] parms = {pro.getId()};
		int line = super.executeUpdate(sql, parms);
		return line;
	}
	//改
	public int gai(ProjectInfo pro) {
		String sql = "UPDATE project_info SET project_name=?,start_date=?,end_date=?,status=? WHERE id = ?";
		Object[] parms = {pro.getProjectName(),pro.getStartDate(),pro.getEndDate(),pro.getStatus(),pro.getId()};
		int line = super.executeUpdate(sql, parms);
		return line;
	}
	@Override
	public List<ProjectInfo> qureyAllByPage(Page page) {
		List<ProjectInfo> pros = new ArrayList<>();
		String sql = "SELECT * FROM project_info limit ?,?";
		Object[] parms = {page.getStartRow(),page.getPageSize()};
		rs = super.executeQuery(sql, parms);
		try {
			while(rs.next()) {
				ProjectInfo pro = new ProjectInfo(rs.getInt("id"), 
													rs.getString("project_name"), 
													rs.getString("start_date"), 
													rs.getString("end_date"), 
													rs.getInt("status"));
				pros.add(pro);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, ps, rs);
		}
		return pros;
	}
	public int getTotalCount() {
		int count = 0;
		String sql = "SELECT COUNT(1) FROM project_info";
		rs=super.executeQuery(sql);
		try {
			while(rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
}
